package com.tigerlee.stopmusic;

import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.OnWheelClickedListener;
import kankan.wheel.widget.OnWheelScrollListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.ArrayWheelAdapter;
import kankan.wheel.widget.adapters.NumericWheelAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class StopMusicActivity extends AbstractActivity implements OnClickListener,
		OnWheelChangedListener, OnWheelClickedListener, OnWheelScrollListener {
	
	private boolean mTimeScrolled;
	private boolean mTimeChanged;

	private final int ID_START_SERVICE = 1;
	private final int ID_TOGGLE_BUTTON = 2;

	private final int ID_WHEEL_HOUR = 1;
	private final int ID_WHEEL_MIN = 2;
	private final int ID_WHEEL_AMPM = 3;
	private final int ID_WHEEL_DURATION = 4;
	
	private String TAG = "StopMusicActivity";
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// abstract class get a running state
		if (mIsRunning == true){
			startActivity(new Intent(this, StopProgressActivity.class));
			finish();
		}	
		// All layout set here
		setLayout();
	}
	@Override
	protected void onResume() {
		super.onResume();
	}
	@Override
	protected void onPause() {
		super.onPause();
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
	};
	
	private void setLayout() {
		// Set ContentView here.
		setWheelView(mIsSetTime);
		// Set Time panel view.
		setTimePanelView(mIsSetTime);
		// All view set here independent wheelView
		setButtons();
		
		setAdView();
	}

	public void setWheelView(boolean isSetTime) {
		if (isSetTime) {// Set Time wheelView
			setContentView(R.layout.activity_start_alarm_time);
			WheelView hours = (WheelView) findViewById(R.id.hour);
			hours.setViewAdapter(new NumericWheelAdapter(this, 1, 12));
			hours.setCyclic(true);
			hours.setId(ID_WHEEL_HOUR);

			WheelView mins = (WheelView) findViewById(R.id.mins);
			mins.setViewAdapter(new NumericWheelAdapter(this, 0, 59, "%02d"));
			mins.setCyclic(true);
			mins.setId(ID_WHEEL_MIN);
			
			
			WheelView ampm = (WheelView) findViewById(R.id.ampm);
			String[] items = {"AM", "PM"};
			ampm.setViewAdapter(new ArrayWheelAdapter<String>(this, items));
			ampm.setId(ID_WHEEL_AMPM);

			// set current time
			int hour = mAlarm.getCurrentHour();
			if(hour >= 12) {
				mAlarm.setAMPM(false);
				// 12 o'clock(PM) is just 12 o'clock by 24-hour clock
				if(hour > 12) hour -= 12;
			}else{
				mAlarm.setAMPM(true);
				// 0 o'clock by 24-hour clock is 12 o'clock(AM)
				if(hour == 0) hour = 12;
			}
			// wheel starting from zero so -1
			hours.setCurrentItem(hour-1);
			mins.setCurrentItem(mAlarm.getCurrentMinute());
			ampm.setCurrentItem(mAlarm.getAMPMByInt());

			// set default alarm time to current time
			hour = hours.getCurrentItem();
			// wheel starting from zero so +1
			mAlarm.setAlarmHour(mAlarm.convertTimeClockTo24Clock(hour+1));
			mAlarm.setAlarmMinute(mins.getCurrentItem());
			
			// add listeners
			hours.addChangingListener(this);
			mins.addChangingListener(this);
			ampm.addChangingListener(this);

			hours.addClickingListener(this);
			mins.addClickingListener(this);
			ampm.addClickingListener(this);

			hours.addScrollingListener(this);
			mins.addScrollingListener(this);
			ampm.addScrollingListener(this);
		} else {// Set duration wheelView
			setContentView(R.layout.activity_start_alarm_min);
			WheelView wheelView = (WheelView) findViewById(R.id.movemin);
			// set a format of string of duration
			wheelView.setViewAdapter(new NumericWheelAdapter(this, 1, 12,
					getString(R.string.text_pre_min_later) + "%d0"
							+ getString(R.string.text_post_min_later)));
			wheelView.setId(ID_WHEEL_DURATION);

			// add listeners
			wheelView.addChangingListener(this);
			wheelView.addClickingListener(this);
			wheelView.addScrollingListener(this);

			// set default position
			wheelView.setCurrentItem(0);
			// set default minute value (0->10 mins)
			mAlarm.setAlarmMinute(0);
			mAlarm.setDurationMinuteAndChange(wheelView.getCurrentItem());
		}
		
	}
	
	private void setButtons() {
		Button button = (Button) findViewById(R.id.button_start);
		button.setId(ID_START_SERVICE);
		button.setOnClickListener(this);

		Button toggleButton = (Button) findViewById(R.id.button_toggle);
		toggleButton.setId(ID_TOGGLE_BUTTON);
		toggleButton.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case ID_START_SERVICE:
			// Create all alarmManager services..
			WidgetControlReceiver.startAlarmService(getApplicationContext(), mAlarm.getAlarmTimeMillis(), false);
			// Update my widget
			WidgetControlReceiver.updateWidget(getApplicationContext());
			finish();
			break;
		case ID_TOGGLE_BUTTON:
			mIsSetTime = mIsSetTime == true ? false : true;
			mPreference.setTimerState(mIsSetTime);
			setLayout();
		default:
			break;
		}
	}

	@Override
	public void onScrollingStarted(WheelView wheel) {
		mTimeScrolled = true;
	}

	@Override
	public void onScrollingFinished(WheelView wheel) {
		if(mIsRunning) return;
		mTimeScrolled = false;
		mTimeChanged = true;
		switch (wheel.getId()) {
		case ID_WHEEL_HOUR:
			int hour = wheel.getCurrentItem()+1;
			mAlarm.setAlarmHour(mAlarm.convertTimeClockTo24Clock(hour));			
			break;
		case ID_WHEEL_MIN:
			mAlarm.setAlarmMinute(wheel.getCurrentItem());
			break;
		case ID_WHEEL_AMPM:
			mAlarm.setAMPM(wheel.getCurrentItem() == 0 ? true : false); 
			break;
		case ID_WHEEL_DURATION:
			mAlarm.setDurationMinuteAndChange(wheel.getCurrentItem());
			break;
		default:
			break;
		}
		mTimeChanged = false;

		// Calculate Time & Set TextView
		setTimePanelView(mIsSetTime);
	}

	@Override
	public void onItemClicked(WheelView wheel, int itemIndex) {
		wheel.setCurrentItem(itemIndex, true);
	}

	@Override
	public void onChanged(WheelView wheel, int oldValue, int newValue) {
		if(mIsRunning) return;
		if (!mTimeScrolled) {
			mTimeChanged = true;
			switch (wheel.getId()) {
			case ID_WHEEL_HOUR:
				int hour = wheel.getCurrentItem()+1;
				mAlarm.setAlarmHour(mAlarm.convertTimeClockTo24Clock(hour));
				break;
			case ID_WHEEL_MIN:
				mAlarm.setAlarmMinute(wheel.getCurrentItem());
				break;
			case ID_WHEEL_AMPM:
				mAlarm.setAMPM(wheel.getCurrentItem() == 0 ? true : false);
				break;
			case ID_WHEEL_DURATION:
				mAlarm.setDurationMinuteAndChange(wheel.getCurrentItem());
				break;
			default:
				break;
			}
			mTimeChanged = false;
		}
	}
}
